<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Module extends MX_Controller{
        
        protected $_templates;
        protected $_module_name = "";
        
        function __construct() {
            parent::__construct();

            $this->_module_name = $this->router->fetch_module();
            $this->pre_message ="";
            $this->lang->load('access');
            $this->load->model('module_model','module');
            //$this->permit_library->check_permit();
            $this->session->set_userdata(array('Url'=>uri_string()));          
        }
        function index($page=0){
            $this->acl->check('view','','',base_url());
            
            $data['title'] = lang('module.list');
            if($this->acl->check('add'))
                $data['add']=$this->_module_name.'/module/add';
            
            $data['_module_name']=$this->_module_name;
            
            $config['base_url'] = base_url().$this->_module_name.'/permission/index';  
            $data['num'] = $this->module->getNumModule();
            $config['total_rows'] = $data['num'];
            $config['per_page']  = 20;
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);
            $data['pagination']    = $this->pagination->create_links();
            
            $data['list'] = $this->module->getModulePaging($config['per_page'],$page);
            
            $this->_templates['page'] = 'module/list';
            $this->site_lib->load($this->_templates['page'],$data);
        }
        
        private function buildData($data){
          $updateData=array();
          $updateData['module_code']    = $data['module_code'];
          $updateData['module_name']    = $data['module_name'];
          $updateData['module_comment'] = $data['module_comment'];

          return $updateData;
        }

        /*
         * @author : Vo Van Hieu
         * @method : add new module
         * @date   : 21/06/2013
         * @modify : 
         */
        function add(){
            $this->acl->check('add','','',base_url());
            
            $data['title'] = lang('module.add');

            if(!empty($_POST['bt_submit'])){
               $this->form_validation->set_rules('module_code',lang('module.code'),'trim|required');  
               $this->form_validation->set_rules('module_name',lang('module.name'),'trim|required'); 
               
               if($this->form_validation->run() == FALSE){
                  $this->pre_message = validation_errors();
               }else{
                   $code = $this->input->post('module_code');
                    if($this->module->check_code($code)){
                        $dataSave = $this->buildData($this->input->post());
                        if($this->module->saveData(0,$dataSave)){
                           $this->session->set_flashdata('message',lang('admin.save_successful'));
                           redirect($this->_module_name.'/module');
                        }else{
                           $this->pre_message = lang('admin.save_unsuccessful');
                           redirect($this->_module_name.'/module/add');
                        }
                    }else{
                        $this->pre_message = lang('admin.code_exist');
                    }
               } 
            }
            $data['message'] = $this->pre_message;          
            $this->_templates['page'] = 'module/add';
            $this->site_lib->load($this->_templates['page'],$data);
        }
          /*
         * @author : Vo Van Hieu
         * @method : edit module
         * @date   : 19/10/2012
         * @modify : 
         */
        function edit($id){
          $this->acl->check('edit','','',base_url());  
            
          $data['title'] = lang('module.edit');
          $data['rs'] = $this->module->getModuleByID($id);
          if(!empty($_POST['bt_submit'])){
             $this->form_validation->set_rules('module_code',lang('module.code'),'trim|required');  
             $this->form_validation->set_rules('module_name',lang('module.name'),'trim|required');
             
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                 $code = $this->input->post('module_code');
                 if($this->module->check_code($code,$id)){
                     $dataSave=$this->buildData($this->input->post());
                    if($this->module->saveData($id,$dataSave)){
                        $this->session->set_flashdata('message',lang('admin.save_successful'));
                        redirect($this->_module_name.'/module');
                    }else{
                        $this->pre_message = lang('admin.save_unsuccessful');
                        redirect($this->_module_name.'/edit/'.$id);
                    }
                 }else{
                     $this->pre_message = lang('admin.code_exist');
                 }
             } 
          }
          $data['message'] = $this->pre_message;          
          $this->_templates['page'] = 'module/edit';
          $this->site_lib->load($this->_templates['page'],$data);
        }  
        /*
         * @author : Vo Van Hieu
         * @method : delete module
         * @date   : 19/10/2012
         * @modify : 
         */
        function del($id){
            $this->acl->check('del','','',base_url());
            //$page = (int)$this->input->post('page');

            if($this->module->delete($id))
                $this->session->set_flashdata('message',lang('admin.delete_successful'));
            else 
                $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));

            redirect($this->_module_name.'/module');
        }
        /*
         * @author : Vo Van Hieu
         * @method : delete modules
         * @date   : 19/10/2012
         * @modify : 
         */
        function dels(){
            $this->acl->check('dels','','',base_url());
            
            if(!empty($_POST['ar_id']))
            {
                //$page = (int)$this->input->post('page');
                $ar_id = $this->input->post('ar_id');

                if(!empty($_POST['btn_submit']))
                {
                    for($i = 0; $i < sizeof($ar_id); $i ++) {
                        if ($ar_id[$i]){
                            if($this->module->delete($ar_id[$i]))
                                $this->session->set_flashdata('message',lang('admin.delete_successful'));
                            else 
                                $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));
                        }
                    }
                }
            }
            redirect($this->_module_name.'/module');
        }
    }
/* End of file module.php */
/* Location: ./application/webadmin/modules/system/controllers/module.php */
